package com.web;

import com.dao.FoodDao;
import com.dao.Impl.FoodImpl;
import com.pojo.Callfood;
import com.pojo.Food;
import com.utils.webUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class AdminServlet extends HttpServlet {
    FoodDao foodDao=new FoodImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action=req.getParameter("action");
        if("list".equals(action)){
            List<Food> foods=foodDao.queryFoods();
            req.setAttribute("foods",foods);
            req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
        }else if ("add".equals(action)){
            ///666操作---webUtils封装了中文字符串转换的工具类---------------------
            String name = webUtils.getNewString(req.getParameter("name"));
            Food food = webUtils.copyParamToBean(req.getParameterMap(), new Food());
            food.setName(name);
            ///666操作------------------------------------------------------
            foodDao.addFood(food);
            resp.sendRedirect(req.getContextPath() + "/adminServlet?action=list");
        }else if ("delete".equals(action)) {
            String idd = req.getParameter("id");
            int id = Integer.parseInt(idd);
            foodDao.deleteFoodById(id);
            resp.sendRedirect(req.getContextPath() + "/adminServlet?action=list");
        }else if("calllist".equals(action)){
            List<Callfood> foods=foodDao.queryCallFoods();
            req.setAttribute("callfoods",foods);
            req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
        }
    }
}
